package elevatronic.view.simulation;

import java.util.ArrayList;
import java.util.List;

import elevatronic.models.Elevator;
import elevatronic.models.People;
import elevatronic.utils.Utils;
import elevatronic.view.ImageView;

/**
 * 
 * @author Benjamin CORNEC
 * elevator view of elevator model
 */
public class ElevatorView {
	public static final int FIRST_ELEVATOR_POS_X=297;
	public static final int STEP_BETWEEN_ELEVATOR_AND_FLOOR=5;	
	public static final int SEPARATION_BETWEEN_TWO_ELEVATOR=60;

	//elevator model
	private Elevator elevator;
	public Elevator getElevator() {
		return elevator;
	}
	public void setElevator(Elevator elevator) {
		this.elevator = elevator;
	}
	
	//previous floor, where the elevator came from
	private int oldFloorId;
	public int getOldFloorId() {
		return oldFloorId;
	}
	public void setOldFloorId(int oldFloorId) {
		this.oldFloorId = oldFloorId;
	}
	
	//next floor, where the elevator go, will stop
	public int getTargetFloorId(){
		return getElevator().getTargettedFloor().getNumber();
	}
	
	//where is the elevator, what elevator, between which elevators
	public double getFloorLocalization(){
		double retour=0;
		double remainingTime=getElevator().getRemainingTime();
		double timeToMakeTheTrip=getElevator().getTimeToMakeTheTrip();
		if (timeToMakeTheTrip!=0){
			double progression=(timeToMakeTheTrip-remainingTime)/timeToMakeTheTrip;
			retour=(getTargetFloorId()-getOldFloorId())*progression+getOldFloorId();
		}
		return retour;
	}
	
	
	//contain all people in the elevator
	private List<PeopleView> peopleView;
	public List<PeopleView> getPeopleView() {
		return peopleView;
	}
	public void setPeopleView(List<PeopleView> peopleView) {
		this.peopleView = peopleView;
	}
	
	
	//image of the elevator
	private ImageView imageElevator;
	public ImageView getImageElevator() {
		return imageElevator;
	}
	public void setImageElevator(ImageView imageElevator) {
		this.imageElevator = imageElevator;
	}
	
	//constructor
	public ElevatorView(Elevator elevator){
		setElevator(elevator);
		setImageElevator(new ImageView("elevatorOpen",0,0,36,43));
		setPeopleView(new ArrayList<PeopleView>());
		for (People people:elevator.getPeopleInElevator()){
			getPeopleView().add(new PeopleView(people));
		}
		setLocalisation();
	}
	
	
	/**
	 * updatePeopleViewList is used to refresh the peopleView list of the elevator, indeed, people go in and out everytime, we have to refresh who are in
	 */
	public void updatePeopleViewList(){
		List<PeopleView> morePeopleView = new ArrayList<PeopleView>();
		List<PeopleView> lessPeopleView = new ArrayList<PeopleView>();		
		//if people is not present anymore in simulation, we delete it in the view
		for(PeopleView peopleView:getPeopleView()){
			//<search the people>
			boolean found=getElevator().getPeopleInElevator().contains(peopleView.getPeople());
			//</search the people>
			if (!found){
				lessPeopleView.add(peopleView);
			}
		}
		
		//if any people of the simulation is not present in the view, we add it
		for (People people:getElevator().getPeopleInElevator()){
			//<search the people>
			boolean found=false;
			for(PeopleView peopleView:getPeopleView()){
				if (people==peopleView.getPeople()){
					found=true;
					break;
				}
			}
			//</search the people>
			if (!found){
				morePeopleView.add(new PeopleView(people));
			}			
		}
		
		//modify the list
		getPeopleView().removeAll(lessPeopleView);
		getPeopleView().addAll(morePeopleView);
	}
	
	
	//iteration of the elevator
	public void update(int step){	
		updatePeopleViewList();
		reLocalisation();
	}
	
	/**
	 * This method allow to set X and Y on the screen
	 */
	private void setLocalisation(){
		getImageElevator().setPosX(FIRST_ELEVATOR_POS_X+(getElevator().getId()*SEPARATION_BETWEEN_TWO_ELEVATOR));
		getImageElevator().setPosY(Utils.GetPosY(getFloorLocalization(), getImageElevator().getHght())-STEP_BETWEEN_ELEVATOR_AND_FLOOR);
	}	
	
	/**
	 * This method allow to set X and Y on the screen
	 */
	private void reLocalisation(){
		getImageElevator().setPosY(Utils.GetPosY(getFloorLocalization(), getImageElevator().getHght())-STEP_BETWEEN_ELEVATOR_AND_FLOOR);
	}
	
}
